﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DLV.Data.Entities;
using DLV.FrameWork;
using DLV.Library.Utilities;

namespace DLV.BackEnd.Pages
{
    public partial class EditPartner : AdminPageBase
    {
        protected string ThumbnailPreview = String.Empty;

        private PartnerInfo _partnerInfo = new PartnerInfo();
        private readonly PartnerController _partnerController = new PartnerController();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(Request.QueryString["PartnerId"]))
                _partnerInfo = _partnerController.GetPartner(int.Parse(Request.QueryString["PartnerId"]));

            if (Page.IsPostBack) return;

            LoadAddressDefault();
            LoadContinent();
            LoadAdminUser();
            LoadStatus();

            if (_partnerInfo.PartnerId == -1) return;

            LoadNational();
            LoadCity();
            LoadDistrict();
            LoadWard();

            RenderForm();
        }
        protected void lbtSave_Click(object sender, EventArgs e)
        {
            SaveData();
        }
        protected void lbtClose_Click(object sender, EventArgs e)
        {
            Response.Redirect("/Pages/ListPartner.aspx?xml=Partner");
        }

        private void SaveData()
        {
            _partnerInfo.PartnerName = txtPartnerName.Text;
            _partnerInfo.PartnerSortName = txtPartnerSortName.Text;
            _partnerInfo.PartnerEnglishName = txtPartnerEnglishName.Text;
            _partnerInfo.Logo = thumbnailAvatar.Value;
            _partnerInfo.Telephone = txtTelephone.Text;
            _partnerInfo.Phone = txtPhone.Text;
            _partnerInfo.Hotline = txtHotline.Text;
            _partnerInfo.Fax = txtFax.Text;
            _partnerInfo.Email = txtEmail.Text;
            _partnerInfo.Represent = txtRepresent.Text;
            _partnerInfo.Regency = txtRegency.Text;
            _partnerInfo.ContinentId = Convert.ToInt32(ddlContinent.SelectedValue);
            _partnerInfo.NationalId = Convert.ToInt32(Request["ctl00$placeHolderContent$ddlNational"]);
            _partnerInfo.CityId = Convert.ToInt32(Request["ctl00$placeHolderContent$ddlCity"]);
            _partnerInfo.DistrictId = Convert.ToInt32(Request["ctl00$placeHolderContent$ddlDistrict"]);
            _partnerInfo.WardId = Convert.ToInt32(Request["ctl00$placeHolderContent$ddlWard"]);
            _partnerInfo.Address = txtAddress.Text;
            _partnerInfo.Status = int.Parse(ddlStatus.SelectedValue);

            if (_partnerInfo.PartnerId == -1)
            {
                _partnerInfo.CreatedBy = ddlAdminUser.SelectedValue == "-1"
                    ? UserId
                    : int.Parse(ddlAdminUser.SelectedValue);
                _partnerInfo.CreatedDate = DateTime.Now;
                _partnerController.InsertPartner(_partnerInfo);
            }
            else
            {
                _partnerInfo.UpdatedBy = UserId;
                _partnerInfo.UpdatedDate = DateTime.Now;
                _partnerController.UpdatePartner(_partnerInfo);
            }
            Response.Redirect("/Pages/ListPartner.aspx?xml=Partner");
        }

        private void RenderForm()
        {
            txtPartnerName.Text = _partnerInfo.PartnerName;
            txtPartnerSortName.Text = _partnerInfo.PartnerSortName;
            txtPartnerEnglishName.Text = _partnerInfo.PartnerEnglishName;

            ThumbnailPreview = String.Format("<img src='{0}' />", ImageUpload.GetImageUrl(200, _partnerInfo.Logo, false, ""));
            thumbnailAvatar.Value = _partnerInfo.Logo;

            txtTelephone.Text = _partnerInfo.Telephone;
            txtPhone.Text = _partnerInfo.Phone;
            txtHotline.Text = _partnerInfo.Hotline;
            txtFax.Text = _partnerInfo.Fax;
            txtEmail.Text = _partnerInfo.Email;
            txtRepresent.Text = _partnerInfo.Represent;
            txtRegency.Text = _partnerInfo.Regency;
            ddlContinent.SelectedValue = _partnerInfo.ContinentId.ToString();
            ddlNational.SelectedValue = _partnerInfo.NationalId.ToString();
            ddlCity.SelectedValue = _partnerInfo.CityId.ToString();
            ddlDistrict.SelectedValue = _partnerInfo.DistrictId.ToString();
            ddlWard.SelectedValue = _partnerInfo.WardId.ToString();
            txtAddress.Text = _partnerInfo.Address;
            ddlAdminUser.SelectedValue = _partnerInfo.CreatedBy.ToString();
            ddlStatus.SelectedValue = _partnerInfo.Status.ToString();
        }
        private void LoadAddressDefault()
        {
            ddlNational.Items.Insert(0, new ListItem("--- Chọn quốc gia ---", "-1"));
            ddlCity.Items.Insert(0, new ListItem("--- Chọn tỉnh/thành phố ---", "-1"));
            ddlDistrict.Items.Insert(0, new ListItem("--- Chọn quận/huyện ---", "-1"));
            ddlWard.Items.Insert(0, new ListItem("--- Chọn phường/xã ---", "-1"));
        }
        private void LoadContinent()
        {
            var ds = new DataSet();
            ds.ReadXml(MapPath("~/xml/Continent.xml"));
            var dv = ds.Tables["Continent"].DefaultView;
            ddlContinent.DataTextField = "Name";
            ddlContinent.DataValueField = "ID";
            ddlContinent.DataSource = dv;
            ddlContinent.DataBind();

            ddlContinent.Items.Insert(0, new ListItem("--- Chọn châu lục ---", "-1"));
        }
        private void LoadNational()
        {
            ddlNational.DataSource = new NationalController().ListNationalByContinent(_partnerInfo.ContinentId);
            ddlNational.DataTextField = "NationalName";
            ddlNational.DataValueField = "NationalId";
            ddlNational.DataBind();

            ddlNational.Items.Insert(0, new ListItem("--- Chọn quốc gia ---", "-1"));
        }
        private void LoadCity()
        {
            ddlCity.DataSource = new CityController().ListCityByNational(_partnerInfo.NationalId);
            ddlCity.DataTextField = "CityName";
            ddlCity.DataValueField = "CityId";
            ddlCity.DataBind();

            ddlCity.Items.Insert(0, new ListItem("--- Chọn tỉnh/thành phố ---", "-1"));
        }
        private void LoadDistrict()
        {
            ddlDistrict.DataSource = new DistrictController().ListDistrictByCity(_partnerInfo.CityId);
            ddlDistrict.DataTextField = "DistrictName";
            ddlDistrict.DataValueField = "DistrictId";
            ddlDistrict.DataBind();

            ddlDistrict.Items.Insert(0, new ListItem("--- Chọn quận/huyện ---", "-1"));
        }
        private void LoadWard()
        {
            ddlWard.DataSource = new WardController().ListWardByDistrict(_partnerInfo.DistrictId);
            ddlWard.DataTextField = "WardName";
            ddlWard.DataValueField = "WardId";
            ddlWard.DataBind();

            ddlWard.Items.Insert(0, new ListItem("--- Chọn phường/xã ---", "-1"));
        }
        private void LoadAdminUser()
        {
            ddlAdminUser.DataSource = new AdminUserController().ListAdminUser();
            ddlAdminUser.DataTextField = "Username";
            ddlAdminUser.DataValueField = "UserID";
            ddlAdminUser.DataBind();

            ddlAdminUser.Items.Insert(0, new ListItem("--- Chọn người tạo ---", "-1"));
        }
        private void LoadStatus()
        {
            var ds = new DataSet();
            ds.ReadXml(MapPath("~/xml/Status.xml"));
            var dv = ds.Tables["Status"].DefaultView;
            ddlStatus.DataTextField = "Name";
            ddlStatus.DataValueField = "ID";
            ddlStatus.DataSource = dv;
            ddlStatus.DataBind();
        }
    }
}